package com.woniuxy.microsysserver.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.MemberEntity;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microsysserver.entity.BankCardEntity;
import com.woniuxy.microsysserver.entity.MyPage;
import java.util.Map.Entry;

import com.woniuxy.microsysserver.service.MemberService;
import com.woniuxy.microsysserver.uitl.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.*;

/**
 * Auther: wuson  <br/>
 * Date: 2021/4/15:20:04  <br/>
 * Description:
 */
@RefreshScope
@Api("银行卡管理")
@Slf4j
@RequestMapping("/sys/onlin")
@RestController
public class OnlinMemberController {
    @Autowired(required = false)
    private HashOperations hashOperations;
    @Autowired(required = false)
    private MemberService memberService;

    @ApiOperation("分页查看在线用户")
    @PostMapping(value = "/onlinmember",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean addOne(@RequestBody MyPage myPage){

        //开始分页
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        //查询结果
        List<MemberEntity> memberEntities = new ArrayList<>();
        Map onlineMember = hashOperations.entries("onlineMember");
        //Map集合循环遍历 通过迭代器的方式
        Iterator<Entry<String, Object>> it = onlineMember.entrySet().iterator();
        while(it.hasNext()){
            Entry<String, Object> entry = it.next();
            MemberEntity currentMember = memberService.selectByAccount(entry.getKey());
            currentMember.setLoginTime(entry.getValue().toString());
            memberEntities.add(currentMember);
        }
        PageInfo<MemberEntity> pageInfo = new PageInfo<>(memberEntities);
        return ResultBean.builder().code(200).msg(Constant.SUCCESSFUL).data(pageInfo).build();
    }


    @ApiOperation("退出登录")
    @PostMapping(value = "/quit",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean quit(@RequestBody MemberEntity currentMemberEntity){
            if(currentMemberEntity==null || currentMemberEntity.getUsername()==null){
                return ResultBean.ParamError();
            }
        hashOperations.delete("onlineMember", currentMemberEntity.getUsername());

        return ResultBean.builder().code(200).msg(Constant.SUCCESSFUL).build();
    }

}
